home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 548 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: itnews.sc.intel.com!news    
  2. From: Eric Minor <EricX_Minor@ccm.sc.intel.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Problem with polymorphism.
  5. Date: 5 Jan 1996 02:27:20 GMT
  6. Organization: Intel - Internet Engineering Group    
  7. Message-ID: <4ci2a8$hhi@itnews.sc.intel.com>
  8. References: <4cgjkr$fcr@oznet07.ozemail.com.au>
  9. NNTP-Posting-Host: eminorx-desk.sc.intel.com
  10.  
  11.  
  12. regarding your polymorphism problem...
  13.  
  14. You seem to have a grasp of what the problem is.  You're right,
  15. you can't call TKey::GetKey() because it doesn't exist.  It
  16. doesn't make sense to me to start your polymorphism at the
  17. TKey level when the data you wish to compare doesn't even
  18. exist at that level.  It seems to me you must have the 'int key'
  19. data member defined at the TKey level.  You mention that causes
  20. you other problems...I'd be interested to know what that problem
  21. is.  Of course, if you want to access this data member in 
  22. derived classes you would need to put it in the protected
  23. or public sections of TKey.  Also, since you mention that you're
  24. a beginner, I'll chime in that the behavior you are implementing
  25. in the Compare() method might be more typically implemented
  26. with the overridden equality operator with prototype as follows:
  27.  
  28. int TKey::operator==(const TKey& rhs);
  29.  
  30. Perhaps it makes more sense for you to use a separate Compare()
  31. method since you are interested in a tri-state return.
  32.  
  33. > But I don't know what would happen if I was to declare
  34. > the original pure virtual function as: 
  35. > virtual int Compare(void) = 0; 
  36. > and then in all derived classes declare it as:
  37. > virtual int Compare(iKey *key2); 
  38. > etc.  Can I overload virtual functions like that safely?  
  39.  
  40. No you cannot.  Virtual function overrides must have identical
  41. signatures.
  42.  
  43. Hope this helps.
  44.  
  45. - Eric Minor
  46.  
  47.